#region Summary

// 
// Owner : JackieHan 
// 
// Solution : DailyCollection 
// 
// Project : Jackie.Utility.Notify
// 
// File : ClientSubscribeCollection.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 客户端订阅消息标题的集合
// 
// History : 
//			2011-04-26 18:00:32	    Create 
// 

#endregion

#region Using

using System;
using System.Linq;
using System.Collections.Generic;

#endregion

namespace Jackie.Utility.Notify.Domain
{
    /// <summary> 
    /// 客户端订阅消息标题的集合
    /// </summary>
    public class ClientSubscribeCollection
    {
        #region Singleton
        
        private static ClientSubscribeCollection instance;
        private static readonly object LockObject = new object();

        public static ClientSubscribeCollection GetInstance()
        {
            if (instance == null)
            {
                lock (LockObject)
                {
                    if (instance == null)
                        instance = new ClientSubscribeCollection();
                }
            }
            return instance;
        }

        #endregion

        /// <summary>
        /// 订阅消息的所有客户端集合
        /// </summary>
        private readonly IList<ClientSubscribeInfo> clientSubscribes;

        private ClientSubscribeCollection()
        {
            clientSubscribes = new List<ClientSubscribeInfo>();
        }

        public void Add(ClientSubscribeInfo info)
        {
            clientSubscribes.Add(info);
        }

        public void Remove(string clientSign, string title)
        {
            var removedItem =
                clientSubscribes.FirstOrDefault(item => item.Title.Equals(title) && item.ClientSign.Equals(clientSign));
            clientSubscribes.Remove(removedItem);
        }

        public IList<ClientSubscribeInfo> GetClients(string titleCode)
        {
            return clientSubscribes.Where(client => client.Title.Code.Equals(titleCode)).ToList();
        }
    }
}